public class Tree {

    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

        public TreeNode createTree() {
            TreeNode A = new TreeNode('A');
            TreeNode B = new TreeNode('B');
            TreeNode C = new TreeNode('C');
            TreeNode D = new TreeNode('D');
            TreeNode E = new TreeNode('E');
            TreeNode F = new TreeNode('F');
            TreeNode G = new TreeNode('G');
            TreeNode H = new TreeNode('H');

            A.left = B;
            A.right = C;
            B.left = D;
            B.right = E;
            E.right = H;
            C.left = F;
            C.right = G;

            return A; // 根节点

        }


    // 获取树中节点的个数
    public int size(TreeNode root) {
        if (root == null) {
            return 0;
        }

        //从单一子树入手 即可通过递归解决整棵树的问题
        // 遍历左子树， 记录值
        int leftnum = size(root.left);
        // 遍历右子树， 记录值
        int rightnum = size(root.right);

        //调用结束 + 1（即+ 根节点自身）
        return leftnum + rightnum + 1;
    }

        public int size2(TreeNode root) {
            if (root == null) {
                return 0;
            }

            return size(root.left) + size(root.right) + 1;
        }



//     获取叶子节点的个数

    public int getLeafNodeCount(TreeNode root) {
        if(root == null) {
            return 0;
        }

        if(root.left == null && root.right == null) {
            return 1;
        }

        return getLeafNodeCount(root.left)
                + getLeafNodeCount(root.right);
    }

    int getLeafNodeCount3(TreeNode root){
        if(root == null){
           return 0;
        }

        if(root.left == null && root.right == null){
            return 1;
        }

        int leafLeftnum = getLeafNodeCount3(root.left);
        int leafRightnum = getLeafNodeCount3(root.right);
        return leafLeftnum + leafRightnum;

    }

    int getLeafNodeCount2(TreeNode root){
        if(root == null){
            return 0;
        }

        if(root.left == null && root.right == null){
            return 1;
        }

        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);

    }

//    // 子问题思路-求叶子结点个数
// 获取第K层节点的个数
    int getKLevelNodeCount(TreeNode root,int k){
        if(root == null){
            return 0;
        }

        if(k == 1){
            return 1;
        }

        return getKLevelNodeCount(root.left,k - 1) + getKLevelNodeCount(root.right,k - 1);

    }

    int getKLevelNodeCount2(TreeNode root,int k){
        if(root == null) {
            return 0;
        }

        if(k == 1){
            return 1;
        }

        int getKleftnum = getKLevelNodeCount(root.left,k-1);
        int getKrightnum = getKLevelNodeCount(root.right,k-1);

        return getKleftnum + getKrightnum;

    }

    // 获取二叉树的高度
    int getHeight(TreeNode root){
        if(root == null) {
            return 0;
        }

        int getHleft = getHeight(root.left);
        int getHright = getHeight(root.right);

        return Math.max(getHleft,getHright) + 1;

    }

//    int getHeight2(TreeNode root){
//        if(root == null) {
//            return 0;
//        }
//
//        return getHeight2(root.left) > getHeight2(root.right)
//                ? getHeight2(root.left) + 1 : getHeight2(root.right) + 1;
//
//    }
    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, int val){
        if(root == null) {
            return null;
        }

        if(root.val == val){
            return root;
        }

        TreeNode ret = find(root.left,val);
        if(ret != null){
            return ret;
        }

        ret = find(root.right,val);
        if(ret != null){
            return ret;
        }

        return null;


    }


}



